<?php

class generalFunction {

    public function allowedImageFormats() {
        $imageAllowedFormat = Array(
            "image/gif",
            "image/pjpeg",
            "image/jpeg",
            "image/jpg",
            "image/png",
            "image/x-png",
            "image/gif"
        );
        return $imageAllowedFormat;
    }

    public function searchfilter($searchString) {
        $search_array = Array('%', '_');
        $replace_array = Array('\%', '\_');
        return str_replace($search_array, $replace_array, $searchString);
    }

    public function cleanfilter($searchString) {
        $search_array = Array('\%', '\_');
        $replace_array = Array('%', '_');
        return str_replace($search_array, $replace_array, $searchString);
    }

    public function filter($array = '', $filterArray = Array()) {
        /*
          for the filter array the value with  following key will call following functionms
          mysqlRealEscape => mysql_real_escape_string
          htmlEntityDecode => html_entity_decode
          htmlEntity => htmlentities
          trim => TrimElement
          stripslashes => StripSlashElement
         */
        foreach ($filterArray as $filterType) {
            if (is_array($array)) {
                $array = array_map($filterType, $array);
            } else {
                $array = $filterType($array);
            }
        }
        return $array;
    }

    public function redirect($url) {
        if ($url == '') {
            echo "redirect url cannot be empty";
            exit;
        }
        header('Location: ' . $url);
        exit;
    }

    public function ValidUrl($url) {
        $regex = "((https?|ftp)\:\/\/)?"; // SCHEME
        $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
        $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
        $regex .= "(\:[0-9]{2,5})?"; // Port
        $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
        $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
        $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor
        $pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
        if (preg_match("/^$regex$/", $url)) {
            return true;
        }
        return false;
    }

    public function ValidEmailAddress($EmailAddress) {
        if (!preg_match("/^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i", $EmailAddress)) {
            return false;
        }
        return true;
    }

    public function ValidAlphaNumeric($name) {
        if (!preg_match("/^[a-zA-Z0-9 _-]+$/", $name)) {
            return false;
        }
        return true;
    }

    public function isNumeric($n) {
        if (!preg_match("/^[0-9]+$/", $n)) {
            return false;
        }
        return true;
    }

    public function isFloat($string) {
        if (!preg_match("/^[0-9]+(.[0-9]+)?$/", $string)) {
            return false;
        }
        return true;
    }

    public function ValidatePassword($Pwd) {
        if (strlen($Pwd) < 5) {
            return false;
        }
        return true;
    }

    public function processImageUrl($imageName = "", $imageBasePath = "") {
        if ($imageName == "") {
            return "";
        }
        $pos = strpos($imageName, "/");
        if ($pos !== false) {
            if ($pos == '0') {
                return $imageBasePath . $imageName;
            }
        }
        return $imageBasePath . "/" . $imageName;
    }

    public function IsValidDate($str) {
        $stamp = strtotime($str);

        if (!is_numeric($stamp)) {
            return FALSE;
        }
        $month = date('m', $stamp);
        $day = date('d', $stamp);
        $year = date('Y', $stamp);

        if (checkdate($month, $day, $year)) {
            return TRUE;
        }

        return FALSE;
    }

    public function IsGreaterDate($date, $expiry_date) {
        $date = strtotime($date);
        $expiry_date = strtotime($expiry_date);
        if ($expiry_date > $date) {
            return FALSE;
        }
        return TRUE;
    }

    public function IsEmpty($value = '') {
        if (trim($value) == '') {
            return true;
        }
        return false;
    }

}

function mysqlRealEscape($str = '') {
    if ($str == '') {
        return;
    }
    return $str;

    if (is_numeric($data))
        return $data;
    $unpacked = unpack('H*hex', $data);
    return '0x' . $unpacked['hex'];
}

function htmlEntityDecode($str = '') {
    if ($str == '') {
        return;
    }
    return htmlspecialchars_decode($str, ENT_QUOTES);
}

function htmlEntity($str = '') {
    if ($str == '') {
        return;
    }
    return htmlspecialchars($str, ENT_QUOTES);
}

function utfencode($str = '') {
    if ($str == '') {
        return;
    }
    return utf8_encode($str);
}

function utfdecode($str = '') {
    if ($str == '') {
        return;
    }
    return utf8_decode($str);
}

function addslash($str = '') {
    if ($str == '') {
        return;
    }
    return addslashes($str);
}

function stripslash($str = '') {
    if ($str == '') {
        return;
    }
    return stripslashes($str);
}

function TrimElement($str = '') {
    if ($str == '') {
        return;
    }
    return trim($str);
}

function StripSlashElement($str = '') {
    if ($str == '') {
        return;
    }
    return stripslashes($str);
}

function recursivEncodeArray(&$array = array()) {

    foreach ($array as $key => $value) {
        if (is_array($value)) {
            recursivEncodeArray($array[$key]);
        } else {
            $array[$key] = utf8_decode($array[$key]);
        }
    }
}

function getCollapseText($_text, $_length = 150) {
    if (strlen($_text) > $_length) {
        $_temp_text = substr($_text, 0, $_length);
        $_temp_text .="...";
        return $_temp_text;
    } else {
        return $_text;
    }
}

function dl_file($file) {

    //First, see if the file exists
    if (!is_file($file)) {
        die("<div style='text-align:center;'><h1> Sorry, file temporary not available!</h1></div>");
    }

    //Gather relevent info about file
    $len = filesize($file);
    $filename = basename($file);
    $file_extension = strtolower(substr(strrchr($filename, "."), 1));

    //This will set the Content-Type to the appropriate setting for the file
    switch ($file_extension) {
        case "pdf": $ctype = "application/pdf";
            break;
        case "exe": $ctype = "application/octet-stream";
            break;
        case "zip": $ctype = "application/zip";
            break;
        case "doc": $ctype = "application/msword";
            break;
        case "xls": $ctype = "application/vnd.ms-excel";
            break;
        case "ppt": $ctype = "application/vnd.ms-powerpoint";
            break;
        case "gif": $ctype = "image/gif";
            break;
        case "png": $ctype = "image/png";
            break;
        case "jpeg":
        case "jpg": $ctype = "image/jpg";
            break;
        case "mp3": $ctype = "audio/mpeg";
            break;
        case "wav": $ctype = "audio/x-wav";
            break;
        case "mpeg":
        case "mpg":
        case "mpe": $ctype = "video/mpeg";
            break;
        case "mov": $ctype = "video/quicktime";
            break;
        case "avi": $ctype = "video/x-msvideo";
            break;

        //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
        case "php":
        case "htm":
        case "html":
        case "txt": die("<b>Cannot be used for " . $file_extension . " files!</b>");
            break;

        default: $ctype = "application/force-download";
    }

    //Begin writing headers
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");

    //Use the switch-generated Content-Type
    header("Content-Type: $ctype");

    //Force the download
    //$header="Content-Disposition: attachment; filename=".$filename.";"; //for force fully download
    $header = "Content-Disposition: inline; filename=" . $filename . ";";
    header($header);
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . $len);
    @readfile($file);
    exit;
}

?>